﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member.UserControl
{
    public partial class KetQua : System.Web.UI.UserControl
    {
        public static int CurrentPage = 0;
         List<SanPhamDTO> sanPhamDtos = new List<SanPhamDTO>();
        private int countPage = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                
                CurrentPage = 0;
                DataBindDataList();
                DataBindDataListProductsByCategory();
            }

        }
        private void DataBindDataList()
        {
            // Xu ly chan truong hop CatId khong phai so nguyen
            string catId = Request.QueryString["id"];
            int id = 0;
            Int32.TryParse(catId, out id);

            string name = Request.QueryString["name"];
            if(name != "" && catId == "LSP000")
            {
                sanPhamDtos = SanPhamBUS.getToSanPhamOfTenSP(name.Trim());
            }
            if (catId != "LSP000")
            {
                sanPhamDtos = SanPhamBUS.getToSanPhamOfLoaiSP(catId);
            }

            // Thuc hien truy van va hien thi len DataList

            //DataTable table = SanPhamBUS.getToSanPhamOfLoai(catId);

           

            // phan trang
            countPage = sanPhamDtos.Count / 3;
            if (sanPhamDtos.Count % 3 != 0)
            {
                countPage = sanPhamDtos.Count / 3 + 1;
            }


            for (int i = 0; i < countPage; i++)
            {
                HyperLink hlNumberPages = new HyperLink();
                LiteralControl literal = new LiteralControl("&nbsp;&nbsp;");
                hlNumberPages.Text = (i + 1).ToString();
                hlNumberPages.NavigateUrl = string.Format("~/Member/KetQuaTimKiem.aspx?name=" + name + "&id=" + catId + "&pgn=" + (i + 1));
                Panel1.Controls.Add(hlNumberPages);
                Panel1.Controls.Add(literal);

            }
        }

        private void DataBindDataListProductsByCategory()
        {
            string page = Request.QueryString["pgn"];
            int pgn = 0;
            Int32.TryParse(page, out pgn);

            List<SanPhamDTO> sanphamDTO = GetSanPhamListForPage(pgn-1, 3, sanPhamDtos);
            if (sanphamDTO.Count > 0)
            {
                // Thiet lap phan trang
                //PagedDataSource pds = new PagedDataSource();
                //pds.AllowPaging = true;
                //pds.DataSource = sanPhamDtos;
                //pds.PageSize = 3;
                //pds.CurrentPageIndex = CurrentPage;

                DataList1.DataSource = sanphamDTO;
                DataList1.DataBind();
               
               
                //Hien Prev hoac Next button neu nhu CurrentPage o trang dau hoac cuoi
                if (pgn == 1)
                {
                    lbTruoc.Visible = false;
                }
                else
                {
                    lbTruoc.Visible = true;
                }
                if (pgn == countPage)
                {
                    lbSau.Visible = false;
                }
                else
                {
                    lbSau.Visible = true;
                }
               
            }
            else
            {
                lbSau.Visible = false;
                lbTruoc.Visible = false;
            }
          
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string catId = Request.QueryString["id"];
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);

            Response.Redirect("~/Member/KetQuaTimKiem.aspx?id=" + catId + "&pgn=" + (pgns - 1));
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            string catId = Request.QueryString["id"];
            string page = Request.QueryString["pgn"];
            int pgns = 0;
            Int32.TryParse(page, out pgns);

            Response.Redirect("~/Member/KetQuaTimKiem.aspx?id=" + catId + "&pgn=" + (pgns + 1));
           
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imageButton = (ImageButton)sender;

            Response.Redirect("~/Member/ChiTietSanPham.aspx?id=" + imageButton.CommandArgument.ToString().Trim());
        }

        private List<SanPhamDTO> GetSanPhamListForPage(int pageIdx, int pageSize, List<SanPhamDTO> srcList)
        {
            int start = pageIdx * pageSize;
            int end = start + pageSize;
            if (end > srcList.Count)
            {
                end = srcList.Count;
            }

            List<SanPhamDTO> slicedList = new List<SanPhamDTO>();
            for (int i = start; i < end; i++)
            {
                slicedList.Add(srcList[i]);
            }
            return slicedList;
        }


    }
}